package fnord.jovian.tango;

import java.awt.*;
import java.util.*;

public class TheText implements TheObject {

	private double _fontSize, _x, _y, _w, _h, _padding = 0;
	private boolean _bold, _center;
	private Vector<String> _text;


	public TheText(String text, double fontSize, double w, double h, boolean bold, boolean center) {
		_text = new Vector<String>();
		_text.add(text);
		_fontSize = fontSize;
		_x = 0;
		_y = 0;
		_w = w;
		_h = h;
		_bold = bold;
		_center = center;
	}

	@Override
	public void paint(Graphics2D g) {
		if (_bold)
			g.setFont(new Font("Arial", Font.BOLD, (int)Math.round(_fontSize * Toolkit.getDefaultToolkit().getScreenResolution() / 72.0)));
		else
			g.setFont(new Font("Arial", Font.PLAIN, (int)Math.round(_fontSize * Toolkit.getDefaultToolkit().getScreenResolution() / 72.0)));
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		FontMetrics fm = g.getFontMetrics();
		int fontHeight = fm.getAscent() + fm.getDescent();
		int fontWidth;
		for (int i = 0; i < _text.size(); i++) {
			fontWidth = fm.stringWidth(_text.get(i));
			if (fontWidth >= _w - _padding*2) {
				resizeText(fm);
				break;
			}
		}
		g.setColor(Color.WHITE);
		for (int i = 0; i < _text.size(); i++) {
			fontWidth = fm.stringWidth(_text.get(i));
			if (_center)
				g.drawString(_text.get(i), (int)(_x + (_w - fontWidth)/2.0),
						(int)(_y + fm.getAscent() + fontHeight*(double)i + (_h - fontHeight*(double)_text.size())/2.0));
			else
				g.drawString(_text.get(i), (int)(_x + _padding),
						(int)(_y + fm.getAscent() + fontHeight*(double)i + (_h - fontHeight*(double)_text.size())/2.0));
		}
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
	}

	public void resizeText(FontMetrics fm) {
		StringTokenizer st = new StringTokenizer(getText());
		_text = new Vector<String>();
		String currentLine = "";
		while(st.hasMoreTokens()) {
			String word = st.nextToken();
			if (!currentLine.equals("") && fm.stringWidth(currentLine + " " + word) < _w - _padding*2)
				currentLine += " " + word;
			else {
				if (!currentLine.equals("")) {
					_text.add(currentLine);
				}
				if (fm.stringWidth(word) < _w - _padding*2) {
					currentLine = word;
				}
				else {
					Vector<String> v = splitString(fm,word);
					for (int i = 0; i < v.size()-1; i++) {
						_text.add(v.get(i));
					}
					currentLine += v.get(v.size()-1);
				}
			}
		}
		if (!currentLine.equals("")) {
			_text.add(currentLine);
		}
	}

	public void setPadding(double padding) {
		_padding = padding;
	}

	public Vector<String> splitString(FontMetrics fm, String s) {
		Vector<String> v = new Vector<String>();
		String currS = s;
		while(true) {
			if (fm.stringWidth(currS) < _w - _padding*2) {
				v.add(currS);
				break;
			}
			for (int i = currS.length()-1; i >= 0; i--) {
				String subS = currS.substring(0, i) + "-";
				if (fm.stringWidth(subS) < _w - _padding*2) {
					v.add(subS);
					currS = currS.substring(i);
					break;
				}
			}
		}
		return v;
	}

	@Override
	public boolean contains(Point p) {
		return true;
	}

	@Override
	public boolean containsClick(Point p) {
		return true;
	}

	@Override
	public boolean containsDrag(Point p) {
		return true;
	}

	@Override
	public boolean containsUp(Point p) {
		return true;
	}

	@Override
	public double getX() {
		return _x;
	}

	@Override
	public double getY() {
		return _y;
	}

	@Override
	public double getCenterX() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public double getCenterY() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setLocation(double x, double y) {
		_x = x;
		_y = y;
	}

	@Override
	public double getWidth() {
		return _w;
	}

	@Override
	public double getHeight() {
		return _h;
	}

	public void setSize(double w, double h) {
		_w = w;
		_h = h;
	}

	public void setText(String text) {
		_text = new Vector<String>();
		_text.add(text);
	}

	public String getText() {
		String t = "";
		if (_text.size() > 0) {
			t += _text.get(0);
			for (int i = 1; i < _text.size(); i++){
				if (t.substring(t.length()-1).equals("-"))
					t += " " + _text.get(i);
				else
					t += _text.get(i);
			}
		}
		return t;
	}

}
